//
//  FixedViewController.m
//  WKWebViewDemo
//
//  Created by migu-hfy on 16/3/29.
//  Copyright © 2016年 migu-hfy. All rights reserved.
//

#import "FixedViewController.h"
#import <WebKit/WebKit.h>

@interface FixedViewController ()

@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic, strong) UIWebView *uiWebView;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *rightItem;
@end

@implementation FixedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self rightAction:self.rightItem];
}

- (IBAction)rightAction:(id)sender {
    self.webView.hidden = !self.webView.hidden;
    
    self.uiWebView.hidden = !self.webView.hidden;
    
    self.rightItem.title = self.webView.hidden ? @"WKWebView" : @"UIWebView";
}

- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
        [self.view addSubview:_webView];
        
        _webView.scrollView.bounces = YES;
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Fixed" ofType:@"html"];
        NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
        
        [_webView loadHTMLString:htmlString baseURL:[NSBundle mainBundle].bundleURL];
    }
    return _webView;
}

- (UIWebView *)uiWebView
{
    if (!_uiWebView) {
        _uiWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)];
        [self.view addSubview:_uiWebView];
        
        _uiWebView.scrollView.bounces = YES;
        
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Fixed" ofType:@"html"];
        NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
        
        [_uiWebView loadHTMLString:htmlString baseURL:[NSBundle mainBundle].bundleURL];
    }
    return _uiWebView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
